const formatTime = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
const formatTime1 = date => {
  if (typeof (date) != 'object') {
    if (date.indexOf('T') > -1) { } else {
      date = date.replace(/-/g, '/')
    }
  }
  // 备选方案
  // date = String(date)
  // date = date.replace(/T/g, ' ')
  //  date = date.replace(/-/g, '/')
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours();
  const minute = date.getMinutes();
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatTime2 = date => {
  if (typeof (date) != 'object') {
    if (date.indexOf('T') > -1) { } else {
      date = date.replace(/-/g, '/')
    }
  }
  // 备选方案
  // date = String(date)
  // date = date.replace(/T/g, ' ')
  //  date = date.replace(/-/g, '/')
  date = new Date(date)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hour = String(date.getHours()).padStart(2, '0');
  const minute = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return year + '年' + month + '月' + day + '日' + ' ' + hour + ':' + minute + ':' + seconds
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDistance(lat1, lng1, lat2, lng2) {
  console.log("-----经纬度-----", lat1, lat2, lng1, lng2)
  var radLat1 = lat1 * Math.PI / 180.0;
  var radLat2 = lat2 * Math.PI / 180.0;
  var a = radLat1 - radLat2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;

}
function GetQueryString(url) {
  if (url.indexOf("?") != -1) {
    var index = url.indexOf('?') + 1;
    // 得到？后的字符串
    var str = url.substr(index); // postid=10457794&actiontip=保存修改成功')
    var paramsObj = {};
    // 字符串通过&标识，转为数组
    var arrs = str.split("&"); // ["postid=10457794", "actiontip=保存修改成功"]
    for (var i = 0; i < arrs.length; i++) {
      // 分别将 = 左右两边拆分为数组, 动态变为键值对
      paramsObj[arrs[i].split("=")[0]] = arrs[i].split("=")[1]
      //  arrs[i].split("=")   ["postid", "10457794"]
    }
  }
  return paramsObj
}

function getMonday(date) {
  let day = new Date(date).getDay() || 7;
  let mondayTime = new Date(date).getTime() - ((day - 1) * 24 * 60 * 60 * 1000);
  return formatTime(mondayTime);
}

const hour = date => {
  const date1 = new Date(date)
  const day = date1.getDate();
  const hour = date1.getHours()
  const minute = date1.getMinutes()
  const second = date1.getSeconds()
  console.log([hour + day * 24, minute, second].map(formatNumber).join(':'))
  return [hour + day * 24, minute, second].map(formatNumber).join(':')
}
const getTime = function (startTime, mark) {
  startTime = startTime.replace(/-/g, '/')
  var newDate = new Date().getTime();
  var reciprocal = (new Date(startTime).getTime() - newDate) / 1000;
  if (reciprocal < 0) {
    return '';
  }

  // 获取天、时、分、秒
  let day = parseInt(reciprocal / (60 * 60 * 24));
  if (day > 0) {
    return `${day}天`
  }
  let hour = parseInt(reciprocal % (60 * 60 * 24) / 3600);
  let minute = parseInt(reciprocal % (60 * 60 * 24) % 3600 / 60);
  let second = parseInt(reciprocal % (60 * 60 * 24) % 3600 % 60);
  if (mark == 'newjoblist') {
    return [hour + day * 24, minute].map(formatNumber).join(':')
  }
  return [hour + day * 24, minute, second].map(formatNumber).join(':')
}

const getAge = (UUserCard) => {
  var myDate = new Date();
  var month = myDate.getMonth() + 1;
  var day = myDate.getDate();
  var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
  if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
    age++;
  }
  return age
}
function getRpx(){ 
  var winWidth = wx.getSystemInfoSync().windowWidth;
   return 750/winWidth;
}
const birthdayInAge = (str)=>{
	if (!str) return false;
	return new Date().getFullYear() - str.substring(0,4);
}
const getaatime = (str)=>{
	let date = str.replace(/-/g, '/');
	if(formatTime(new Date())==formatTime(new Date(date))){
		return '今天';
	} else if (new Date().getTime()-new Date(date).getTime()<60*60*24*1000){
		return '昨天';
	}else{
		return formatTime(new Date(date)).substr(5,10)
	}
}
module.exports = {
  formatTime: formatTime,
  formatTime1,
  formatTime2,
  formatNumber,
  getDistance: getDistance,
  GetQueryString: GetQueryString,
  getMonday,
  hour,
  getTime,
  getAge,
  birthdayInAge,
  getRpx,
  getaatime
}